package com.tang.excel.oci.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tang.excel.oci.domain.Product;
import com.tang.excel.oci.mapper.ProductMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Tang
 * @classname ExcelReadListener
 * @description [  ]
 * @date 2021/3/24 9:11
 */
@Slf4j
public class ProductExcelReadListener extends AnalysisEventListener<Product> {

    private List<Product> productList = new ArrayList<>(16);

    ProductMapper productMapper;

    public ProductExcelReadListener(ProductMapper productMapper) {
        this.productMapper = productMapper;
    }

    /**
     * 每解析一条数据都会调用
     * @param product
     * @param analysisContext
     */
    @Override
    public void invoke(Product product, AnalysisContext analysisContext) {
        product.setOperateTime(new Date());
        product.setOperator("admin");
        product.setKohCompany(0.0);
        productList.add(product);
    }

    /**
     * 所有数据解析完成了 都会来调用
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("所有数据解析完成");
        productMapper.batchInsert(productList);
    }
}